//
//  MTImagePickViewController.m
//  ShiShiBang
//
//  Created by loujianwei on 16/9/29.
//  Copyright © 2016年 motianjituan. All rights reserved.
//

#import "MTImagePickViewController.h"

@interface MTImagePickViewController ()
@property(nonatomic,copy)void(^selectBlock)(UIImage * selectImg);
@property(nonatomic,copy)void(^cancelBlock)();
@end

@implementation MTImagePickViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //从系统相册选择一张图片
    //弹出系统相册
    UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];
    //设置照片来源
    pickVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    //modal
    [self presentViewController:pickVC animated:YES completion:nil];
}

#pragma mark -UIImagePickerControllerDelegate
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
//
//    [self dismissViewControllerAnimated:YES completion:nil];
//
//    UIImage *image = info[UIImagePickerControllerOriginalImage];
//
//    //把图片转成二进制/Users/loujianwei/Desktop/.DS_Store
//    NSData *data =  UIImagePNGRepresentation(image);
//
//    //    [data writeToFile:@"/Users/loujianwei/Desktop/photo.png" atomically:YES];
//
//}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"选择");
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        [picker dismissViewControllerAnimated:YES completion:^{
            if (_selectBlock) {
                _selectBlock(image);
            }
        }];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    NSLog(@"您取消了选择图片");
    [picker dismissViewControllerAnimated:YES completion:^{
        if (_cancelBlock) {
            _cancelBlock();
        }
    }];
}
@end
